<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>Catalyst config YAML</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="Template_customized_Filte.html">Template customized Filters</a>&nbsp;&nbsp;>>Next: <a href="Accessing_External_TTFile.html">导入 Template Toolkit 文件</a></p>
<h1>Catalyst config YAML</h1>
<div class='content'>
<p>Category: <a href='Catalyst.html'>Catalyst</a> &nbsp; Keywords: <b>Catalyst YAML</b></p>我们一般写代码都应当避免一种称为 hardcode 硬代码。<br />所谓的 hard code 是指将代码拷贝到另外的地方你必须更改其中的某些代码。<br />比如你写了一个 Controller, 里面使用了类如 '/home/fayland/eplanet' 这样的字符串。当你将这个代码迁移到其他目录或其他机子的时候，人们必须要改变这个字符串才能正常运行该程序。我们管这些字符串为 hard code.<br />一种避免 hard code 的方法就是将所有的这些字符串用一个配置文件包括，这样人们就只需要修改下配置文件就能运行程序而不是修改程序的源代码。<br />这种避免 hard code 的方法尤其在源码协作/ Subversion 时最为有用。每人拥有相同的源码不同的配置文件，这样不会对源码造成冲突。<p />最近的 <a href='http://catalyst.perl.org/calendar'>CatalystAdvent</a> 就介绍了怎么在 <a href="http://dev.catalyst.perl.org">Catalyst</a> 中使用 <a href='http://catalyst.perl.org/calendar/2005/9'>Day 9 - YAML, YAML, YAML!</a>.<br />原来在 MyApp.pm 中这么写的代码：<br /><pre> __PACKAGE__->config( name => 'MyApp', 'View::<a href="http://www.template-toolkit.org">TT</a>' => { EVAL_PERL => 1 } );</pre>将修改为：<br /><pre>use YAML ();    <br />__PACKAGE__->config( YAML::LoadFile( __PACKAGE__->path_to('myapp.yml') ) );</pre>而 myapp.yml 文件的结构为：<br /><pre>---<br />name: MyApp<br />View::TT:<br />  EVAL_PERL: 1</pre>不过该 Advent 里没有介绍怎么写 yml 文件。对于复杂的希哈里套数组再套希哈什么的结构很容易让人写错。<br />我一般的习惯都是写一个 pl 文件来创立该 yml 文件。比如在 Person 项目中我在 tools 里是这么写的：<br /><pre>#!/usr/bin/perl<br />use strict;<br />use warnings;<br />use FindBin;<br />use YAML();<p /># CHANGE THIS LINE, THEN RUN IT!<br />my $DIR = $FindBin::Bin;<br />$DIR =~ s!/tools/?!!; # /usr/local/apache/www/Person<br />print $DIR;<p />my %a = (<br /> &nbsp; &nbsp;name => 'Person',<br /> &nbsp; &nbsp;root => "$DIR/root",<br /> &nbsp; &nbsp;templates => "$DIR/templates", <br /> &nbsp; &nbsp;member_images => "$DIR/root/member",<br /> &nbsp; &nbsp;email => {<br /> &nbsp; &nbsp; &nbsp; &nbsp;enable    => 1, # local machine can be 0<br /> &nbsp; &nbsp; &nbsp; &nbsp;charset   => 'utf8',<br /> &nbsp; &nbsp; &nbsp; &nbsp;smtp_host => '61.152.95.132',<br /> &nbsp; &nbsp; &nbsp; &nbsp;prefix    => '[<a href="http://www.perlchina.org">PerlChina</a>]',<br /> &nbsp; &nbsp;},<br /> &nbsp; &nbsp;dsn           => 'dbi:mysql:person',<br />    dsn_user      => 'root',<br />    dsn_password  => '',<br />    images_folder => "$DIR/captcha/images",<br /> &nbsp; &nbsp;data_folder   => "$DIR/captcha/data",<br /> &nbsp; &nbsp;output_folder => "$DIR/root/captcha",<br /> &nbsp; &nbsp;# if u run this in your local machine, CHANGE IT<br /> &nbsp; &nbsp;base_site     => 'http://localhost:3000/',<br />);<p />YAML::DumpFile("$DIR/Person.yaml", \%a);<br />print ', DONE!';<p />1;</pre>这样我们用 YAML::DumpFile 出一个 yml 文件，然后用 LoadFile 导进一个。非常的绝配。 :-)<br />详细的查看 <a href="http://dev.perlchina.org/cgi-bin/trac.cgi/browser/trunk/Person/tools/YAML_Create.pl">http://dev.perlchina.org/cgi-bin/trac.cgi/browser/trunk/Person/tools/YAML_Create.pl</a> 和 <a href="http://dev.perlchina.org/cgi-bin/trac.cgi/browser/trunk/Person/lib/Person.pm">http://dev.perlchina.org/cgi-bin/trac.cgi/browser/trunk/Person/lib/Person.pm</a></div>
<p><&lt;Previous: <a href="Template_customized_Filte.html">Template customized Filters</a>&nbsp;&nbsp;>>Next: <a href="Accessing_External_TTFile.html">导入 Template Toolkit 文件</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=Catalyst%20config%20YAML&url=http://www.fayland.org/journal/Catalyst_YAML.html'>+Del.icio.us</a></p>
<strong>Related items</strong>
<ul><li><a href='050328.html'>Catalyst & Eplanet</a> < <span class='digit'>2005-03-28 13:19:19</span> ></li><li><a href='050423.html'>Day [05.4.23] stay with me</a> < <span class='digit'>2005-04-23 16:17:02</span> ></li><li><a href='Catalyst_Authenticate.html'>Catalyst Authenticate</a> < <span class='digit'>2005-09-26 12:50:39</span> ></li><li><a href='Catalyst_Flaw.html'>Catalyst 的一个不足（一个已去掉）</a> < <span class='digit'>2005-09-27 13:44:31</span> ></li><li><a href='Catalyst_Session_Win32.html'>Catalyst 在 Win32 下的 Session</a> < <span class='digit'>2005-09-29 11:08:04</span> ></li><li><a href='Catalyst_NoFavIcon.html'>Catalyst && favicon.ico</a> < <span class='digit'>2005-09-30 00:57:57</span> ></li><li><a href='Catalyst_Overview.html'>我对 Catalyst 的理解和介绍</a> < <span class='digit'>2005-10-08 01:39:40</span> ></li><li><a href='Catalyst_XMLRPC.html'>Catalyst && XML-RPC</a> < <span class='digit'>2005-10-11 21:50:30</span> ></li><li><a href='CatalystAdventCalendar_CN.html'>Catalyst Advent Calendar 中文版</a> < <span class='digit'>2005-12-15 23:28:40</span> ></li></ul>
Created on <span class="digit">2005-12-10 12:50:39</span>, Last modified on <span class="digit">2005-12-10 12:53:23</span><br />
Copyright 2004-2005 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.62.
</body>
</html>